package com.ctg.itrdc.sysmgr.portal.control;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.portal.api.dto.FuncCompDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.FuncMenuDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IFuncCompService;
import com.ctg.itrdc.sysmgr.portal.api.service.IFuncMenuService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.data.entity.FuncMenu;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

@Controller
@RequestMapping("/func_menu")
public class FuncMenuController extends SysmgrCURDController<Long, FuncMenuDTO> {

	@Resource
	private IFuncMenuService funcMenuService;
	@Resource
	private IFuncCompService funcCompService;
	
	@RequestMapping("/sub_menus")
	@ResponseBody
	public CommonResult getMenusByParent(FuncMenuDTO funcMenuDTO, @RequestParam(required = false) String grantObjType,
			@RequestParam(required = false) Long grantObjId) {
		//copy from selectSubMenu
		List<FuncMenuDTO> menuList = funcMenuService.selectSubMenuList(grantObjType, grantObjId, funcMenuDTO);
		Boolean hadComp = funcMenuDTO.getHadComp();
		if (hadComp == null || hadComp) {
			// TODO 待优化
			for (FuncMenuDTO dto : menuList) {
				FuncCompDTO funcCompDTO = new FuncCompDTO();
				funcCompDTO.setMenuId(dto.getMenuId());
				Boolean existComp = funcCompService.existComp(null, null, funcCompDTO);
				if (existComp) {
					dto.setHadComp(true);
				} else {
					dto.setHadComp(false);
				}
			}
		}
		FuncCompDTO funcCompDTO = new FuncCompDTO();
		funcCompDTO.setMenuId(funcMenuDTO.getParMenuId());
		List<FuncCompDTO> compList = funcCompService.selectCompList(grantObjType, grantObjId, funcCompDTO);

		ArrayList<Object> list = new ArrayList<Object>();
		if (CollectionUtils.isNotEmpty(menuList)) {
			list.addAll(menuList);
		}
		if (CollectionUtils.isNotEmpty(compList)) {
			list.addAll(Sets.newHashSet(compList));
		}
		return new CommonResult(HttpStatus.OK.toString(), "数据查询成功", list);
	}

	/**
	 * @api {get} /func_menu/sub_menu 用户子菜单查询
	 * @apiGroup func_menu
	 * @apiName sub_menu
	 * @apiParam {Long} parMenuId
	 * @apiParam {String} sysCode
	 * @apiParam {String} grantObjType
	 * @apiParam {Long} grantObjId
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/sub_menu")
	@ResponseBody
	public CommonResult selectSubMenu(FuncMenuDTO funcMenuDTO, @RequestParam(required = false) String grantObjType,
			@RequestParam(required = false) Long grantObjId) {

		List<FuncMenuDTO> menuList = funcMenuService.selectSubMenuList(grantObjType, grantObjId, funcMenuDTO);
		Boolean hadComp = funcMenuDTO.getHadComp();
		if (hadComp == null || hadComp) {
			// TODO 待优化
			for (FuncMenuDTO dto : menuList) {
				FuncCompDTO funcCompDTO = new FuncCompDTO();
				funcCompDTO.setMenuId(dto.getMenuId());
				Boolean existComp = funcCompService.existComp(grantObjType, grantObjId, funcCompDTO);
				if (existComp) {
					dto.setHadComp(true);
				} else {
					dto.setHadComp(false);
				}
			}
		}
		return new CommonResult(HttpStatus.OK.toString(), "数据查询成功", menuList);
	}

	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid FuncMenuDTO dto, BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功", funcMenuService.insert(dto));
	}

	@RequestMapping("/update")
	@ResponseBody
	public CommonResult updateByPrimaryKey(@Valid FuncMenuDTO dto, BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		funcMenuService.updateByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "更新数据成功");
	}

	@RequestMapping("/delete")
	@ResponseBody
	public CommonResult deleteByPrimaryKey(FuncMenuDTO dto) throws Exception {
		CommonResult check = checkSubMenus(dto);
		if (check != null) {
			return check;
		}

		funcMenuService.deleteByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}

	/**
	 * 批量删除，不推荐使用
	 * 
	 * @param ids
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/batch/delete")
	@ResponseBody
	public CommonResult batchDeleteByPrimaryKey(String ids, FuncMenuDTO dto) throws Exception {
		List<FuncMenuDTO> dtoList = Lists.newArrayList();
		String[] idStr = ids.split(",");
		for (String i : idStr) {
			Long id = Long.parseLong(i);
			FuncMenuDTO tmp = new FuncMenuDTO();
			tmp.setMenuId(id);
			
			CommonResult check = checkSubMenus(tmp);
			if (check != null) {
				return check;
			}
			
			dtoList.add(tmp);
		}
		
		funcMenuService.batchDeleteByPrimaryKey(dtoList);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}
	
	private CommonResult checkSubMenus(FuncMenuDTO dto) {
		FuncMenuDTO funcMenuDTO = new FuncMenuDTO();
		funcMenuDTO.setParMenuId(dto.getMenuId().longValue());
		List<FuncMenuDTO> children = funcMenuService.selectByExample(funcMenuDTO);
		if (children != null && children.size() != 0) {
			return new CommonResult(HttpStatus.INTERNAL_SERVER_ERROR.toString(), "请先删除菜单的下级菜单");
		}
		return null;
	}

}
